<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1num }"
            @click="options1fn(index, item.min, item.max)"
            v-for="(item, index) in options1"
            :key="index"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: index == options2num }"
            @click="options2fn(index, item.type)"
            v-for="(item, index) in options2"
            :key="index"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodlistShow" />

      <p style="text-align: center; color: red; margin-top: 20px">
        {{ isTextbotBoom ? "已经加载完毕..." : "正在加载中..." }}
      </p>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { SerchAPI } from "@/request/api";
import { getClientHeight, getScrollHeight, getScrollTop } from "@/utils/index";
export default {
  data() {
    return {
      did: 0,
      type: 0,
      min: 0,
      max: 0,
      keyword: "",
      // 产品列表
      goodlist: [],
      // 显示产品列表的数组
      goodlistShow: [],
      // 默认展示第一页
      page: 1,
      // 每一页展示的个数
      size: 8,
      // false 表示没有正在加载
      isloading: false,
      //是否已经到低
      isTextbotBoom: false,
      // 排序的数组项
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1num: 0,
      options2num: 0,
      // 分类数组项
      options2: [
        { type: 0, txt: "全部", isActive: true },
        { type: 1, txt: "实体商品", isActive: false },
        { type: 2, txt: "虚拟商品", isActive: false },
      ],
    };
  },
  components: {
    Crumb,
    List,
  },

  created() {
    // console.log("keyword:", this.$route.query.keyword);
    this.keyword = this.$route.query.keyword;
    this.Goodcar();
  },

  mounted() {
    // 监听滚动事件
    window.addEventListener("scroll", this.scrollFn);
  },
  // 取消滚动事件
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    // 封装全部的商品
    scrollFn() {
      // 1
      console.log(
        "开始滚动了...",
        getScrollTop(),
        getClientHeight(),
        getScrollHeight()
      );
      // 2
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 200) {
        if (this.goodlistShow.length >= this.goodlist.length) {
          // 没有数据就返回
          this.isTextbotBoom = true;
          return;
        }

        if (this.isloading == false) {
          this.isloading = true;
          console.log("加载新的数据");
          // 从goodlist拿八条新数据到goodShow里面
          this.page++; //加载下一页的数据
          // console.log(this.page, this.size); //每一页的八条
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.size * this.page;
              i++
            ) {
              console.log("i的值:", i, this.goodlist[i]);
              if (this.goodlist[i]) {
                this.goodlistShow.push(this.goodlist[i]);
              }
            }
            this.isloading = false; //表示已经加载完了
          }, 1000);
        }
      }
    },

    async Goodcar() {
      let res = await SerchAPI({
        did: this.did,
        type: this.type,
        min: this.type,
        max: this.max,
        keyword: this.keyword,
      });
      if (!res) return;
      // console.log("值为:", res.data);
      this.goodlist = res.data;
      this.goodlistShow = this.goodlist.filter((item, index) => index < 8);

      // 初始化判断当内容只有一页的时候的是否加载完毕
      this.isTextbotBoom = false;
      this.page = 1;
      // 判断新数据是否到达底部
      if (this.goodlistShow.length >= this.goodlist.length) {
        // 没有数据之后的显示
        this.isTextbotBoom = true;
        // return;
      }
    },
    options1fn(index, min, max) {
      // console.log(1);
      this.options1num = index;
      this.min = min;
      this.max = max;

      this.Goodcar();
    },
    options2fn(index, type) {
      // console.log(2);
      this.options2num = index;
      this.options2.forEach((item) => (item.isActive = false));
      this.options2[index].isActive == true;
      this.type = type;
      this.Goodcar();
    },
  },

  //
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.keyword = newVal;
          this.Goodcar();
        }
      },
    },
  },
};
</script>

<style lang = "less" scoped>
.goods {
  padding-bottom: 50px;
  background: #efefef;

  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          /* color: @base-color; */
          font-weight: bold;
          color: aqua;
        }
      }
    }
  }
}
</style>